﻿#region << 版 本 注 释 >>

/*----------------------------------------------------------------
* 类 名 称 ：PathUtil
* 类 描 述 ：
* 作    者 ：henrychang
* 创建时间 ：2019/1/27 13:01:05
*******************************************************************
* Copyright @ henrychang 2019. All rights reserved.
*******************************************************************
//----------------------------------------------------------------*/

#endregion << 版 本 注 释 >>

using System.IO;
using UnityEngine;

namespace TouchFish.ToolClass
{
    public static class PathUtil
    {
        public static byte[] ReadFile(string path)
        {
            using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                var bytes = new byte[fs.Length];
                fs.Read(bytes, 0, (int)fs.Length);
                fs.Close();
                return bytes;
            }
        }

        public static void WriteFile(string path, byte[] bytes)
        {
            var directory = Path.GetDirectoryName(path);
            path.CreateDirIfNotExists();
            using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                fs.Seek(0, SeekOrigin.End);
                fs.Write(bytes, 0, bytes.Length);
                fs.Close();
            }
        }

        public static byte[] ReadFileFromPersistentDataPath(string path)
        {
            path = Application.persistentDataPath + "/" + path;
            return ReadFile(path);
        }

        public static void WriteFileToPersistentDataPath(string path, byte[] bytes)
        {
            path = Application.persistentDataPath + "/" + path;
            WriteFile(path, bytes);
        }

        public static string GetStreamingAssetsPath()
        {
#if UNITY_EDITOR || UNITY_IPHONE
            var path = "file://" + Application.streamingAssetsPath;
#else
            string path = Application.streamingAssetsPath;
#endif
            return path;
        }
    }
}